import React from 'react';
import {
  Card, Form,
  Input,
  Button, message,
} from 'antd';
import { PageContainer } from '@ant-design/pro-layout';
import {updateUserPwd} from "@/pages/Admin/service";

const handleFinish = async ({passwordOld:passwordOld,password:password}) => {
  const hide = message.loading('正在处理');
  try {
    const res = await updateUserPwd({
      id: 1,
      passwordOld:passwordOld,
      password:password,
    });
    hide();
    if(res.status === 'ok'){
      message.success(res.message);
    }else {
      message.error(res.message);
    }
    return true;
  } catch (error) {
    hide();
    message.error('操作失败，请重试');
    return false;
  }
};

export default (): React.ReactNode => (
  <PageContainer >
    <Card>
      <Form
        layout="vertical"
        onFinish={handleFinish}
        // initialValues={currentUser}
        hideRequiredMark
        style={{width:300}}
      >
        <Form.Item
          name="passwordOld"
          label="旧密码"
          rules={[
            {
              required: true,
              message: 'Please input your password!',
            },
          ]}

          hasFeedback
        >
          <Input.Password />
        </Form.Item>
        <Form.Item
          name="password"
          label="新密码"
          rules={[
            {
              required: true,
              message: 'Please input your password!',
            },
          ]}
          hasFeedback
        >
          <Input.Password />
        </Form.Item>

        <Form.Item
          name="confirm"
          label="确认密码"
          dependencies={['password']}
          hasFeedback
          rules={[
            {
              required: true,
              message: 'Please confirm your password!',
            },
            ({ getFieldValue }) => ({
              validator(rule, value) {
                if (!value || getFieldValue('password') === value) {
                  return Promise.resolve();
                }
                return Promise.reject('两次密码输入不一致');
              },
            }),
          ]}
        >
          <Input.Password />
        </Form.Item>
        <Form.Item>
          <Button type="primary" htmlType="submit">
            确认修改
          </Button>
        </Form.Item>
      </Form>
    </Card>

  </PageContainer>
);
